package org.project.hint_game;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class CheckSheetActivity extends Activity implements OnClickListener {
	// buttons
	private Button m_submitButton;
	private Button m_backButton;

	// 3 strings for storing data from text files (taken from server)
	private String[] m_checkBoxValues = null;
	private String[] m_cardValues = null;
	private String[] m_player = null;
	// array lists for storing information to edit xml objects
	private ArrayList<Integer> m_imageButtonidArray;
	private ArrayList<Integer> m_imageViewidArray;

	// variable for room switching (tracking state of rooms)
	private int previousRoom_;

	// temp variable to debugging
	private TextView m_tempView;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_check_sheet);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		m_submitButton = (Button) findViewById(R.id.checkSheetSubmitButton);
		m_submitButton.setOnClickListener(this);
		m_backButton = (Button) findViewById(R.id.checkSheetBackButton);
		m_backButton.setOnClickListener(this);

		m_imageButtonidArray = new ArrayList<Integer>();
		m_imageViewidArray = new ArrayList<Integer>();

		m_tempView = (TextView) findViewById(R.id.gnomeNameView);

		//the code for the following 3 is WAY on the bottom of this page!
		// grab data from text file
		readData();
		// setup image buttons with data from text file
		activateImageButtons();
		// setup cards with data from text file
		activateCards();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.checkSheetBackButton:
			goBack();
			break;
		case R.id.checkSheetSubmitButton:
			writeValues();
			break;
		default:
			//if one of the check boxes gets hit
			int id = v.getId();
			changeButton(id);
			break;
		}

	}

	// if the user pushes the back button...go back to the main room activity
	public void goBack() {
		Intent intent = new Intent(this, MainRoomActivity.class);
		intent.putExtra("roomChoice", previousRoom_);
		startActivity(intent);
	}

	// write new values to the txt file
	// calls writeTagsString() which returns a giant string
	// of all the data to be written to the file
	public void writeValues() {
		String data = writeTagsString();
		// m_tempView.setText(data); //debug code!!
		try {
			FileOutputStream fos = openFileOutput("hint_game_data.txt",
					Context.MODE_PRIVATE);
			fos.write(data.getBytes());
			fos.close();
			Toast.makeText(getApplicationContext(), "Changes Saved!",
					Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			m_tempView.setText(e.toString());
		}
	}

	// gets all the data from existing 3 String[] member variables and then
	// returns a concatenated string of the data
	String writeTagsString() {
		String tempdata = "";
		// this first loop is for the player names (doesn't change)
		int size = m_player.length;
		for (int i = 0; i < size; i++) {
			tempdata += m_player[i];
			if (i < size - 1) {
				tempdata += ",";
			}
		}
		// this second loop is for the card values (doesn't change)
		tempdata += ":";
		size = m_imageViewidArray.size();
		for (int i = 0; i < size; i++) {
			tempdata += m_cardValues[i];
			if (i < size - 1) {
				tempdata += ",";
			}
		}
		// this third loop is for check box (image buttons) data (changes
		// according to user input)
		tempdata += ":";
		size = m_imageButtonidArray.size();
		ImageButton temp_button1;
		for (int i = 0; i < size; i++) {
			temp_button1 = (ImageButton) findViewById(m_imageButtonidArray
					.get(i));
			tempdata += temp_button1.getTag().toString();
			if (i < size - 1) {
				tempdata += ",";
			}
		}
		return tempdata;
	}

	// takes in the id of an image button and checks the tag
	// then it changes the tag to a new one as well as the image
	public void changeButton(int value) {
		ImageButton temp_button2 = (ImageButton) findViewById(value);
		String box_name = temp_button2.getTag().toString();
		if (box_name.equals("blank")) {
			temp_button2.setImageResource(R.drawable.box_question);
			temp_button2.setTag("question");
		}
		if (box_name.equals("question")) {
			temp_button2.setImageResource(R.drawable.box_x);
			temp_button2.setTag("x");
		}
		if (box_name.equals("x")) {
			temp_button2.setImageResource(R.drawable.box_knife);
			temp_button2.setTag("knife");
		}
		if (box_name.equals("knife")) {
			temp_button2.setImageResource(R.drawable.box_blank);
			temp_button2.setTag("blank");
		}
	}
	
	// reads in the data from the text file "hint_game_data.txt" and
	// assigns the data to the appropriate String[]
	public void readData() {
		try {
			FileInputStream fis = openFileInput("hint_game_data.txt");
			int c;
			String tempstring = "";
			while ((c = fis.read()) != -1) {
				tempstring += (char) c;
			}
			fis.close();
			String[] values = tempstring.split(":");
			m_player = values[0].split(",");
			m_cardValues = values[1].split(",");
			m_checkBoxValues = values[2].split(",");

//			 the following is debug code!
			 String temp2 = "";
			 for (int i = 0; i<m_cardValues.length; ++i){
			 temp2 += m_cardValues[i];
			 }
//			 m_tempView.setText(temp2);
		} catch (Exception e) {
			m_tempView.setText(e.toString());
		}
	}

	// initial setup of image view array for card images
	public void activateCards() {
		// setup card images
		m_imageViewidArray.add(R.id.card01ImageView);
		m_imageViewidArray.add(R.id.card02ImageView);
		m_imageViewidArray.add(R.id.card03ImageView);
		m_imageViewidArray.add(R.id.card04ImageView);
		m_imageViewidArray.add(R.id.card05ImageView);
		m_imageViewidArray.add(R.id.card06ImageView);
		m_imageViewidArray.add(R.id.card07ImageView);
		m_imageViewidArray.add(R.id.card08ImageView);
		m_imageViewidArray.add(R.id.card09ImageView);
		m_imageViewidArray.add(R.id.card10ImageView);
		m_imageViewidArray.add(R.id.card11ImageView);
		m_imageViewidArray.add(R.id.card12ImageView);
		m_imageViewidArray.add(R.id.card13ImageView);
		m_imageViewidArray.add(R.id.card14ImageView);
		m_imageViewidArray.add(R.id.card15ImageView);
		m_imageViewidArray.add(R.id.card16ImageView);
		m_imageViewidArray.add(R.id.card17ImageView);
		m_imageViewidArray.add(R.id.card18ImageView);
		m_imageViewidArray.add(R.id.card19ImageView);
		m_imageViewidArray.add(R.id.card20ImageView);

		setupImageView();
	}

	// initial setup for cards (image views) (called from activateCards())
	// looks at card values and image view ids and then sets up
	// tags and images accordingly
	public void setupImageView() {
		int size = m_cardValues.length;
		int id = 0;
		ImageView temp_image1;
		for (int i = 0; i < size; ++i) {
			id = m_imageViewidArray.get(i);
			temp_image1 = (ImageView) findViewById(id);
			temp_image1.setTag(m_cardValues[i]);
			setupImage(id);
		}
	}

	// initial setup for image views(cards)
	// called by setupImageView(): takes tag of each recieved id
	// and sets up card image or question box (question box just means
	// that the user does not possess the card...maybe used later for
	// mechnaic that reveals upon being shown that card)
	public void setupImage(int value) {
		ImageView temp_image = (ImageView) findViewById(value);
		String image_name = temp_image.getTag().toString();
		if (image_name.equals("0")) {
			temp_image.setImageResource(R.drawable.box_question);
		}
	}

	// initial setup of image button array for check boxes
	public void activateImageButtons() {
		// setup button images
		m_imageButtonidArray.add(R.id.fairyCheckButton);
		m_imageButtonidArray.add(R.id.ghostCheckButton);
		m_imageButtonidArray.add(R.id.gnomeCheckButton);
		m_imageButtonidArray.add(R.id.magicianCheckButton);
		m_imageButtonidArray.add(R.id.sailorCheckButton);
		m_imageButtonidArray.add(R.id.witchCheckButton);
		m_imageButtonidArray.add(R.id.AxeCheckButton);
		m_imageButtonidArray.add(R.id.BowArrowCheckButton);
		m_imageButtonidArray.add(R.id.MaceCheckButton);
		m_imageButtonidArray.add(R.id.PoisonCheckButton);
		m_imageButtonidArray.add(R.id.SpearCheckButton);
		m_imageButtonidArray.add(R.id.SwordCheckButton);
		m_imageButtonidArray.add(R.id.ArmoryCheckButton);
		m_imageButtonidArray.add(R.id.BallroomCheckButton);
		m_imageButtonidArray.add(R.id.BedroomCheckButton);
		m_imageButtonidArray.add(R.id.CourtyardCheckButton);
		m_imageButtonidArray.add(R.id.DininghallCheckButton);
		m_imageButtonidArray.add(R.id.DungeonCheckButton);
		m_imageButtonidArray.add(R.id.KitchenCheckButton);
		m_imageButtonidArray.add(R.id.LibraryCheckButton);

		setupImageButton();
	}

	// initial setup for image buttons (called from activateImageButtons())
	// looks at check box values and image button ids and then sets up
	// listeners and tags accordingly
	public void setupImageButton() {
		int size = m_checkBoxValues.length;
		int id = 0;
		ImageButton temp_button1;
		for (int i = 0; i < size; ++i) {
			id = m_imageButtonidArray.get(i);
			temp_button1 = (ImageButton) findViewById(id);
			temp_button1.setOnClickListener(this);
			temp_button1.setTag(m_checkBoxValues[i]);
			setupButton(id);
		}
	}

	// initial setup for image buttons (check boxes)
	// called by setupImageButton: takes tag of each recieved id
	// and sets up inital check box image
	public void setupButton(int value) {
		ImageButton temp_button2 = (ImageButton) findViewById(value);
		String box_name = temp_button2.getTag().toString();
		if (box_name.equals("blank")) {
			temp_button2.setImageResource(R.drawable.box_blank);
		}
		if (box_name.equals("question")) {
			temp_button2.setImageResource(R.drawable.box_question);
		}
		if (box_name.equals("x")) {
			temp_button2.setImageResource(R.drawable.box_x);
		}
		if (box_name.equals("knife")) {
			temp_button2.setImageResource(R.drawable.box_knife);
		}
	}

	protected void onResume() {
		super.onResume();
		previousRoom_ = this.getIntent().getIntExtra("currentRoom", 0);
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	// this function is to prevent use of the back button
	// this function is EXTREMELY IMPORTANT as use of the back button
	// can screw up turns in the game right now. (as well as
	@Override
	public void onBackPressed() {
	}
}
